package cn.com.inossem.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ModelDialogActivity extends Activity implements OnClickListener {

	LinearLayout llayout, mlayout, level1, level2, level3;
	ImageView mlevel1, mlevel2, mlevel3, pa;
	boolean ispa = false;
	int level = 0;
	TextView wancheng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.model_dialog);

		initview();
	}

	private void initview() {
		llayout = (LinearLayout) findViewById(R.id.llayout);
		llayout.setOnClickListener(this);
		mlayout = (LinearLayout) findViewById(R.id.mlayout);
		mlayout.setOnClickListener(this);
		level1 = (LinearLayout) findViewById(R.id.level1);
		level1.setOnClickListener(this);
		level2 = (LinearLayout) findViewById(R.id.level2);
		level2.setOnClickListener(this);
		level3 = (LinearLayout) findViewById(R.id.level3);
		level3.setOnClickListener(this);

		mlevel1 = (ImageView) findViewById(R.id.mlevel1);
		mlevel2 = (ImageView) findViewById(R.id.mlevel2);
		mlevel3 = (ImageView) findViewById(R.id.mlevel3);
		pa = (ImageView) findViewById(R.id.pa);
		pa.setOnClickListener(this);
		wancheng = (TextView) findViewById(R.id.wancheng);
		wancheng.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.llayout:
			// setResult(2, intent);
			// finish();
			break;
		case R.id.mlayout:
			break;
		case R.id.level1:
			// intent.putExtra("level", 1);
			// intent.putExtra("ispc", ispc);
			// setResult(1, intent);
			// finish();
			level = 13217;
			updateimagebg(level);
			break;
		case R.id.level2:
			// intent.putExtra("level", 2);
			// intent.putExtra("ispc", ispc);
			// setResult(1, intent);
			// finish();
			level = 13218;
			updateimagebg(level);
			break;
		case R.id.level3:
			// intent.putExtra("level", 3);
			// intent.putExtra("ispc", ispc);
			// setResult(1, intent);
			// finish();
			level = 13219;
			updateimagebg(level);
			break;
		case R.id.pa:
			if (ispa == false) {
				ispa = true;
				pa.setBackgroundResource(R.drawable.pa);
			} else {
				ispa = false;
				pa.setBackgroundResource(R.drawable.mpa);
			}
			break;
		case R.id.wancheng:
			if (level == 0) {
				Toast.makeText(this, "等级不能为空!", Toast.LENGTH_LONG).show();
				return;
			}
			intent.putExtra("level", level);
			intent.putExtra("ispa", ispa);
			setResult(1, intent);
			finish();
			break;
		default:
			break;
		}
	}

	private void updateimagebg(int type) {
		mlevel1.setBackgroundResource(R.drawable.level11);
		mlevel2.setBackgroundResource(R.drawable.level21);
		mlevel3.setBackgroundResource(R.drawable.level31);

		switch (type) {
		case 13217:
			mlevel1.setBackgroundResource(R.drawable.level1);
			break;
		case 13218:
			mlevel2.setBackgroundResource(R.drawable.level2);
			break;
		case 13219:
			mlevel3.setBackgroundResource(R.drawable.level3);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {

			return true;

		}

		return super.onKeyDown(keyCode, event);

	}

}
